from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from .models import UserProfile, College, Department


class UserProfileInline(admin.StackedInline):
    """用户信息内联编辑"""
    model = UserProfile
    can_delete = False
    verbose_name = '用户信息'
    verbose_name_plural = '用户信息'


class UserAdmin(BaseUserAdmin):
    """扩展用户管理"""
    inlines = (UserProfileInline,)
    list_display = ('username', 'email', 'first_name', 'last_name', 'get_role', 'get_college_name', 'is_active')
    list_filter = ('is_active', 'is_staff', 'userprofile__role')
    
    def get_role(self, obj):
        """获取用户角色"""
        try:
            return obj.userprofile.get_role_display()
        except UserProfile.DoesNotExist:
            return '未设置'
    get_role.short_description = '角色'
    
    def get_college_name(self, obj):
        """获取学院名称"""
        try:
            return obj.userprofile.college.name if obj.userprofile.college else '-'
        except UserProfile.DoesNotExist:
            return '-'
    get_college_name.short_description = '学院名称'


@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    """用户信息管理"""
    list_display = ('user', 'role', 'get_college_name', 'created_at')
    list_filter = ('role', 'college', 'created_at')
    search_fields = ('user__username', 'user__email', 'college__name')
    readonly_fields = ('created_at', 'updated_at')
    
    def get_college_name(self, obj):
        """获取学院名称"""
        return obj.college.name if obj.college else '-'
    get_college_name.short_description = '学院名称'


@admin.register(Department)
class DepartmentAdmin(admin.ModelAdmin):
    """学部管理"""
    list_display = ('get_name_display', 'description', 'created_at', 'updated_at')
    list_filter = ('name', 'created_at')
    search_fields = ('name', 'description')
    ordering = ('name',)
    fields = ('name', 'description')
    readonly_fields = ('created_at', 'updated_at')


@admin.register(College)
class CollegeAdmin(admin.ModelAdmin):
    """学院管理"""
    list_display = ('name', 'code', 'get_department_name', 'get_users_count', 'get_subprojects_count', 'created_at')
    list_filter = ('department', 'created_at', 'updated_at')
    search_fields = ('name', 'code', 'description')
    ordering = ('department__name', 'name')
    fields = ('name', 'code', 'department', 'description')
    readonly_fields = ('created_at', 'updated_at')
    
    def get_department_name(self, obj):
        """获取学部名称"""
        return obj.department.get_name_display() if obj.department else '-'
    get_department_name.short_description = '所属学部'
    
    def get_users_count(self, obj):
        """获取关联用户数量"""
        return obj.users_count
    get_users_count.short_description = '关联用户数'
    
    def get_subprojects_count(self, obj):
        """获取关联子项目数量"""
        return obj.subprojects_count
    get_subprojects_count.short_description = '关联子项目数'


# 重新注册User模型
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
